<?php
require_once('../../../../wp-config.php');
$charset=get_bloginfo('charset');
header("Content-Type: text/html; charset=$charset");

global $comment, $comments, $post, $wpdb, $user_ID, $user_identity, $user_email, $user_url;


function fail($s) {
	header('HTTP/1.0 500 Internal Server Error');
	echo $s;
	exit;
}

foreach($_POST as $k=>$v) {
	$_POST[$k] = urldecode($v);
}

$comment_post_ID = (int) $_POST['comment_post_ID'];

$post_status = $wpdb->get_var("SELECT comment_status FROM $wpdb->posts WHERE ID = '$comment_post_ID'");

if ( empty($post_status) ) {
	do_action('comment_id_not_found', $comment_post_ID);
	fail('<p id="ajaxerror"><p>'.__('The post you are trying to comment on does not currently exist in the database.','dkret').'</p>');
} elseif ( 'closed' ==  $post_status ) {
	do_action('comment_closed', $comment_post_ID);
	fail('<p id="ajaxerror">'.__('Sorry, comments are closed for this item.','dkret').'</p>');
}

$comment_author       = trim($_POST['author']);
$comment_author_email = trim($_POST['email']);
$comment_author_url   = trim($_POST['url']);
$comment_content      = trim($_POST['comment']);

// If the user is logged in
get_currentuserinfo();
if ( $user_ID ) :
	$comment_author       = addslashes($user_identity);
	$comment_author_email = addslashes($user_email);
	$comment_author_url   = addslashes($user_url);
else :
	if ( get_option('comment_registration') )
		fail('<p id="ajaxerror">'.__('Sorry, you must be logged in to post a comment.','dkret').'</p>');
endif;

$comment_type = '';

if ( get_option('require_name_email') && !$user_ID ) {
	if ( 6 > strlen($comment_author_email) || '' == $comment_author )
		fail('<p id="ajaxerror">'.__('Please fill the required fields (name, email, spam).','dkret').'</p>');
	elseif ( !is_email($comment_author_email))
		fail('<p id="ajaxerror">'.__('Please enter a valid email address.','dkret').'</p>');
}

if ( function_exists('math_comment_spam_protection') ) { // Support for the math-comment-spam-protection Plugin 
  global $mcsp_MathCheckObj, $user_ID, $mcsp_opt;
  if (  ( !isset($user_ID) ) && ( $comment_data['comment_type'] == '' ) ) { // Do not check if the user is registered & do not check trackbacks/pingbacks
  
    // Get actual result
    $actual_result = $_POST[ $mcsp_opt['mcsp_opt_fieldname_mathresult'] ];
    // Get value user has entered
    $value_entered = $_POST[ $mcsp_opt['mcsp_opt_fieldname_useranswer'] ];
    
    // Get input validation result
    $result = $mcsp_MathCheckObj->InputValidation($actual_result, $value_entered);
    
    if (($result) == 'No answer') {
      fail('<p id="ajaxerror">'.__('Please fill the required fields (name, email, spam).','dkret').'</p>');
    } elseif (($result) == 'Wrong answer') {
      fail('<p id="ajaxerror">'.__('You have entered the wrong result in the spam protection field.','dkret').'</p>');
    }
  }
}

if ( '' == $comment_content )
	fail('<p id="ajaxerror">'.__('Please type a comment.','dkret').'</p>');

$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID');

$new_comment_ID = wp_new_comment($commentdata);

if ( !$user_ID ) :
        setcookie('comment_author_' . COOKIEHASH, stripslashes($comment_author), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
        setcookie('comment_author_email_' . COOKIEHASH, stripslashes($comment_author_email), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
        setcookie('comment_author_url_' . COOKIEHASH, stripslashes($comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
endif;

jquery_ajax_test($new_comment_ID);

function jquery_ajax_test($id) {
	global $comment; global $post;
	$comment = get_comment($id);

	ob_start();
	$comments = array($comment);
	include(TEMPLATEPATH . '/comments.php');
	$commentout = ob_get_clean();
	preg_match('#<li(.*?)>(.*)</li>#ims', $commentout, $matches);

	echo "<li ".$matches[1]."><div id=\"newcomment\">".$matches[2]."</div></li>";
}
?>
